package com.dinggao.crossgo.core.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.dinggao.crossgo.core.constant.CommonConstant;

public class SharedPreferencesUtil {

    /**
     * SharedPreferences 取值共通方法(String)
     *
     * @param context 上下文
     * @param fileName 取值的文件名
     * @param valueName 取值的字段名
     * @return 取出的值
     */
    public static String getSharedPreferencesString(Context context, String fileName, String valueName) {
        SharedPreferences sharedPreferences = context.getApplicationContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return sharedPreferences.getString(valueName, "");
    }

    /**
     * SharedPreferences 取值共通方法(Boolean)
     *
     * @param context 上下文
     * @param fileName 取值的文件名
     * @param valueName 取值的字段名
     * @return 取出的值
     */
    public static boolean getSharedPreferencesBoolean(Context context, String fileName, String valueName) {
        SharedPreferences sharedPreferences = context.getApplicationContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(valueName, false);
    }

    /**
     * SharedPreferences 取值共通方法(String)
     */
    public static void setSharedPreferencesString(Context context, String fileName, String valueName, String value) {
        SharedPreferences sharedPreferences = context.getApplicationContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);

        SharedPreferences.Editor edt = sharedPreferences.edit();
        edt.putString(valueName, value);

        edt.apply();
    }

    /**
     * SharedPreferences 取值共通方法(Boolean)
     */
    public static void setSharedPreferencesBoolean(Context context, String fileName, String valueName, boolean value) {
        SharedPreferences sharedPreferences = context.getApplicationContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);

        SharedPreferences.Editor edt = sharedPreferences.edit();
        edt.putBoolean(valueName, value);

        edt.apply();
    }
    /* *
     *清除指定数据
     * @param key
     */
    public static void clearPreferences(Context context,String key) {

        SharedPreferences.Editor mEditor =context.getSharedPreferences(CommonConstant.USERINFO_SHREDPREFERENCE, context.MODE_PRIVATE).edit();
        mEditor.remove(key);
        mEditor.commit();
    }

    /**
     * 清除所有数据
     * @param context
     */
    public static void clear(Context context){
        SharedPreferences sp = context.getSharedPreferences(CommonConstant.USERINFO_SHREDPREFERENCE, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }




}